﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GFMS.MODEL;
using KPIS.GERP.GFMS.IDAL;

namespace KPIS.GERP.GFMS.DAL
{
    public class D_ICM_Bid : IDAL_ICM_Bid
    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);
        ConvertNullable cn = new ConvertNullable();
        

        public D_ICM_Bid()
        {

        }

        public IList<M_ICM_Bid> GetFull(Nullable<int> BgYear, string ReceiveType)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_ICM_Bid> infoList = new List<M_ICM_Bid>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@BgYear", BgYear);
                dbManager.AddParameters(2, "@ReceiveType", ReceiveType);

                strSQL = "SELECT bid.BidId, bid.Code, bid.Name, bid.Descr"
                    + ", bid.PrId, bid.BgYear, bid.BidDate, bid.ReceiveType, bid.DocumentAmount"
                    + ", bid.RecordStatus"
                    + " FROM IcmBid bid"
                    + " WHERE bid.RecordStatus not like @delete_status"
                    + " AND bid.BgYear = @BgYear"
                    + " AND bid.ReceiveType = @ReceiveType";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_ICM_Bid info = new M_ICM_Bid();
                        info.icm_bid_seq = Convert.ToInt32(drc[i]["BidId"]);
                        info.icm_bid_ref_no = drc[i]["Code"].ToString();
                        info.icm_bid_name = drc[i]["Name"].ToString();
                        info.icm_bid_descr = drc[i]["Descr"].ToString();
                        info.icm_pr_seq = cn.ConvertNullInt(drc[i]["PrId"]);
                        info.bgm_year = cn.ConvertNullInt(drc[i]["BgYear"]);
                        info.bid_date = cn.ConvertNullDateTime(drc[i]["BidDate"]);
                        info.receive_type = drc[i]["ReceiveType"].ToString();
                        info.document_amount = cn.ConvertNullDouble(drc[i]["DocumentAmount"]);

                        info.record_status = drc[i]["RecordStatus"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<M_ICM_Bid> GetList(Nullable<int> BgYear, string ReceiveType, string filter)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_ICM_Bid> infoList = new List<M_ICM_Bid>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@BgYear", BgYear);
                dbManager.AddParameters(2, "@ReceiveType", ReceiveType);
                dbManager.AddParameters(3, "@filter", "%" + filter + "%");

                strSQL = "SELECT *"
                    + " FROM (SELECT bid.BidId"
                        + ", concat(bid.Code, ' - ', bid.Name) as Name"
                        + " FROM IcmBid bid"
                        + " WHERE bid.RecordStatus not like @delete_status"
                        + " AND bid.BgYear = @BgYear"
                        + " AND bid.ReceiveType = @ReceiveType"
                        //+ " AND (receive_date_start <= now() or receive_date_start is null)"
                        //+ " AND (receive_date_end >= now() or receive_date_end is null)"
                        + ") t"
                    + (filter.Length > 0 ? " WHERE Name like @filter" : "");

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_ICM_Bid info = new M_ICM_Bid();
                        info.icm_bid_seq = Convert.ToInt32(drc[i]["BidId"]);
                        info.icm_bid_name = drc[i]["Name"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<M_ICM_Bid> GetReceiveList(Nullable<int> BgYear, Nullable<DateTime> receive_date, string filter)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_ICM_Bid> infoList = new List<M_ICM_Bid>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@BgYear", BgYear);
                dbManager.AddParameters(2, "@receive_date", receive_date);
                dbManager.AddParameters(3, "@filter", "%" + filter + "%");

                strSQL = "SELECT *"
                    + " FROM (SELECT bid.BidId"
                        + ", concat(bid.Code, ' - ', bid.Name) as Name"
                        + " FROM IcmBid bid"
                        + " WHERE bid.RecordStatus not like @delete_status"
                        //+ " AND bid.BgYear = @BgYear"
                        //+ " AND bid.ReceiveType = 'S'"
                        + " AND (ReceiveDateStart <= @receive_date or ReceiveDateStart is null)"
                        + " AND (ReceiveDateEnd >= @receive_date or ReceiveDateEnd is null)"
                        + ") t"
                    + (filter.Length > 0 ? " WHERE Name like @filter" : "");

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_ICM_Bid info = new M_ICM_Bid();
                        info.icm_bid_seq = Convert.ToInt32(drc[i]["BidId"]);
                        info.icm_bid_name = drc[i]["Name"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<M_ICM_Bid> GetOfferList(Nullable<int> BgYear, Nullable<DateTime> offer_date, string filter)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_ICM_Bid> infoList = new List<M_ICM_Bid>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@BgYear", BgYear);
                dbManager.AddParameters(2, "@offer_date", offer_date);
                dbManager.AddParameters(3, "@filter", "%" + filter + "%");

                strSQL = "SELECT *"
                    + " FROM (SELECT bid.BidId"
                        + ", concat(bid.Code, ' - ', bid.Name) as Name"
                        + " FROM IcmBid bid"
                        + " WHERE bid.RecordStatus not like @delete_status"
                        //+ " AND bid.BgYear = @BgYear"
                        //+ " AND bid.ReceiveType = 'S'"
                        + " AND (OfferDateStart <= @offer_date or OfferDateStart is null)"
                        + " AND (OfferDateEnd >= @offer_date or OfferDateEnd is null)"
                        + ") t"
                    + (filter.Length > 0 ? " WHERE Name like @filter" : "");

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_ICM_Bid info = new M_ICM_Bid();
                        info.icm_bid_seq = Convert.ToInt32(drc[i]["BidId"]);
                        info.icm_bid_name = drc[i]["Name"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public M_ICM_Bid GetInfo(int seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_ICM_Bid info = new M_ICM_Bid();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@seq", seq);

                strSQL = "SELECT bid.BidId, bid.Code, bid.Name, bid.Descr"
                    + ", bid.PrId, bid.BgYear, bid.BidDate, bid.ReceiveType, bid.DocumentAmount"
                    + ", bid.RecordStatus"
                    + " FROM IcmBid bid"
                    + " WHERE bid.BidId = @seq";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.icm_bid_seq = Convert.ToInt32(drc[0]["BidId"]);
                    info.icm_bid_ref_no = drc[0]["Code"].ToString();
                    info.icm_bid_name = drc[0]["Name"].ToString();
                    info.icm_bid_descr = drc[0]["Descr"].ToString();
                    info.icm_pr_seq = cn.ConvertNullInt(drc[0]["PrId"]);
                    info.bgm_year = cn.ConvertNullInt(drc[0]["BgYear"]);
                    info.bid_date  = cn.ConvertNullDateTime(drc[0]["BidDate"]);
                    info.receive_type = drc[0]["ReceiveType"].ToString();
                    info.document_amount = cn.ConvertNullDouble(drc[0]["DocumentAmount"]);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }
    }
}
